package com.ciwong.epaper.modules.pad.homeworkwidget.widget.remoteviews;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.RemoteViews;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.ModuleContent;
import com.ciwong.epaper.modules.epaper.bean.MyGrade;
import com.ciwong.epaper.modules.pad.contentprovider.MyGradeWidgetProvider;

import java.util.ArrayList;

import static com.ciwong.epaper.modules.pad.contentprovider.MyGradeWidgetProvider.ACTION_JUMP_MYBOOK_GRADE;
import static com.ciwong.epaper.modules.pad.contentprovider.MyGradeWidgetProvider.ACTION_JUMP_MYGRADE_GRADE;
import static com.ciwong.epaper.modules.pad.contentprovider.MyGradeWidgetProvider.ACTION_JUMP_MYRESOURCE_GRADE;
import static com.ciwong.epaper.modules.pad.contentprovider.MyGradeWidgetProvider.ACTION_REFRESH_CLICK_GRADE;
import static com.ciwong.epaper.modules.pad.contentprovider.MyGradeWidgetProvider.ACTION_WIDGET_LOGIN_GRADE;


public class GradeRemoteViews extends RemoteViews {
    private Context mContext;

    private AppWidgetManager mAppWidgetManager;

    private int[] mAppWidgetIds;

    public GradeRemoteViews(Context context){
        super(context.getPackageName(), R.layout.mygradewidgetlayout);
        init(context);
    }
    private void init(Context context) {
        this.mContext = context;
        this.mAppWidgetManager = AppWidgetManager.getInstance(mContext);
        this.mAppWidgetIds = getAppWidgetIds();


    }

    public int[] getAppWidgetIds() {
        ComponentName provider = new ComponentName(mContext, getAppWidgetProvider());
        return mAppWidgetManager.getAppWidgetIds(provider);
    }

    private Class<? extends AppWidgetProvider> getAppWidgetProvider() {
        return MyGradeWidgetProvider.class;
    }

    private Intent getProviderIntent() {
        Intent intent=new Intent();
        intent.setComponent(new ComponentName(mContext,MyGradeWidgetProvider.class));


        return intent;
    }

    public void updateAppWidget() {
        mAppWidgetManager.updateAppWidget(mAppWidgetIds, this);
    }


    /**
     * 判断是否登录
     * @param isLogin  是否登陆
     * @param isbindPhone  是否登陆
     *
     */
    public void checkIsLogin(boolean isLogin, boolean isbindPhone, ArrayList<MyGrade> myGrades) {
        //view 处理
        if (isLogin&&isbindPhone) {
            setViewVisibility(R.id.my_grade_lay, View.VISIBLE);
            setViewVisibility(R.id.grade_widet_img_nodata, View.GONE);
           // setInt(R.id.my_grade_lay, "setBackgroundResource ",R.mipmap.grade_bg);
            setImageViewResource(R.id.book_widet_img,R.mipmap.book_widget_bg);
            setImageViewResource(R.id.res_widget_img,R.mipmap.reswidget_bg);
            setTextViewText(R.id.wordgrade_num,0+"");
            setTextViewText(R.id.sentgrade_num,0+"");
            setTextViewText(R.id.listengrade_num,0+"");
            setTextViewText(R.id.onlinegrade_num,0+"");
            try{
                if(myGrades!=null&&myGrades.size()>0){
                    for (MyGrade m:myGrades) {
                        if(m.getResourceType().equals(ModuleContent.ResourceType.RESOURCE_TYPE_WORD)){
                            setTextViewText(R.id.wordgrade_num,m.getWorkCount()+"");
                        }else if(m.getResourceType().equals(ModuleContent.ResourceType.RESOURCE_TYPE_LESSON)){
                            setTextViewText(R.id.sentgrade_num,m.getWorkCount()+"");
                        }
                        else if(m.getResourceType().equals(ModuleContent.ResourceType.RESOURCE_TYPE_LISTEN_SPEAK)){
                            setTextViewText(R.id.listengrade_num,m.getWorkCount()+"");
                        }
                        else if(m.getResourceType().equals(ModuleContent.ResourceType.RESOURCE_TYPE_ONLINE_ANSWER)){
                            setTextViewText(R.id.onlinegrade_num,m.getWorkCount()+"");
                        }
                    }
                }
            }catch (Exception e){
                e.getStackTrace();
            }

        } else {
            setViewVisibility(R.id.my_grade_lay, View.GONE);
            setViewVisibility(R.id.grade_widet_img_nodata, View.VISIBLE);
            setImageViewResource(R.id.book_widet_img,R.mipmap.book_widget_no_login);
            setImageViewResource(R.id.res_widget_img,R.mipmap.resource_widget_no_login);
            setOnLoninClickPendingIntent(isLogin);
        }
        setOnJumpToGradePendingIntent(isLogin);
        setOnJumpToBookPendingIntent(isLogin);
        setOnJumpToResourcePendingIntent(isLogin);
        setOnRefreshPendingIntent(isLogin);
    }

    /**
     * 设置登录按钮点击事件
     */
    private void setOnLoninClickPendingIntent(boolean isLogin) {
        Intent intent = getProviderIntent();
        intent.setAction(ACTION_WIDGET_LOGIN_GRADE);
        PendingIntent login = PendingIntent.getBroadcast(mContext, 0, intent, 0);
        setOnClickPendingIntent(R.id.my_grade_book_res_lay, login);
    }





    /**
     * 设置跳转我的成绩页面
     */
    private void setOnJumpToGradePendingIntent(boolean isLogin) {
        Intent intent = getProviderIntent();
        if(isLogin){
            intent.setAction(ACTION_JUMP_MYGRADE_GRADE);
            PendingIntent grade = PendingIntent.getBroadcast(mContext, 0, intent, 0);
            setOnClickPendingIntent(R.id.my_grade_lay, grade);
        }else {
            intent.setAction(ACTION_WIDGET_LOGIN_GRADE);
            PendingIntent login = PendingIntent.getBroadcast(mContext, 0, intent, 0);
            setOnClickPendingIntent(R.id.my_grade_lay, login);
        }


    }

    /**
     * 设置跳转我的书桌
     */
    private void setOnJumpToBookPendingIntent(boolean isLogin) {
        Intent intent = getProviderIntent();
        if(isLogin){
            intent.setAction(ACTION_JUMP_MYBOOK_GRADE);
            PendingIntent book = PendingIntent.getBroadcast(mContext, 0, intent, 0);
            setOnClickPendingIntent(R.id.book_widet_img, book);
        }else {
            intent.setAction(ACTION_WIDGET_LOGIN_GRADE);
            PendingIntent login = PendingIntent.getBroadcast(mContext, 0, intent, 0);
            setOnClickPendingIntent(R.id.book_widet_img, login);
        }

    }
    /**
     * 设置跳转我的资源中心
     */
    private void setOnJumpToResourcePendingIntent(boolean isLogin) {
        Intent intent = getProviderIntent();
        if(isLogin){
            intent.setAction(ACTION_JUMP_MYRESOURCE_GRADE);
            PendingIntent resource = PendingIntent.getBroadcast(mContext, 0, intent, 0);
            setOnClickPendingIntent(R.id.res_widget_img, resource);
        }else {
            intent.setAction(ACTION_WIDGET_LOGIN_GRADE);
            PendingIntent login = PendingIntent.getBroadcast(mContext, 0, intent, 0);
            setOnClickPendingIntent(R.id.res_widget_img, login);
        }

    }

    /**
     * 点击刷新页面
     */
    private void setOnRefreshPendingIntent(boolean isLogin) {
        Intent intent = getProviderIntent();
        if(isLogin){
            intent.setAction(ACTION_REFRESH_CLICK_GRADE);
            PendingIntent grade = PendingIntent.getBroadcast(mContext, 0, intent, 0);
            setOnClickPendingIntent(R.id.refresh_img, grade);
        }else {
            intent.setAction(ACTION_WIDGET_LOGIN_GRADE);
            PendingIntent login = PendingIntent.getBroadcast(mContext, 0, intent, 0);
            setOnClickPendingIntent(R.id.refresh_img, login);
        }

    }


    private void getMyGrade(){

    }

    public void loading() {
        setViewVisibility(R.id.iv_widget_loading, View.VISIBLE);
        setViewVisibility(R.id.refresh_img, View.GONE);
        mAppWidgetManager.updateAppWidget(mAppWidgetIds, this);
    }

    public void loadComplete() {
        setViewVisibility(R.id.refresh_img, View.VISIBLE);
        setViewVisibility(R.id.iv_widget_loading, View.GONE);

    }




}
